package com.shanghaionstar.smart.presenter;

import android.os.Bundle;
import android.text.TextUtils;

import com.midea.iot.sdk.cloud.openapi.MSmartSDK;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.SmartConnectingFrg;
import com.shanghaionstar.smart.SmartIntroduceFrg;
import com.shanghaionstar.smart.SmartNoFoundDeviceFrg;
import com.shanghaionstar.smart.adapter.ConfigureArg;
import com.shanghaionstar.smart.iview.ISmartSettingWifiView;
import com.shanghaionstar.smart.util.NetUtil;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * Created by 7828wc on 2016/6/22.
 */
public class SmartWifiSettingPresenter extends BasePresenter<Object> {
    private BaseActivity mContext;
    private ISmartSettingWifiView mSmartAddDeviceView;


    public SmartWifiSettingPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        mSmartAddDeviceView = (ISmartSettingWifiView) view;
        mContext = (BaseActivity) mSmartAddDeviceView.onActivity();
    }



    public void goToFunIntro() {
        SmartIntroduceFrg introFrg = new SmartIntroduceFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(),introFrg,SmartIntroduceFrg.class,true);
    }

    public void goToConnect(String mQRcode,String routerSsid,String routerPw){
        if(TextUtils.isEmpty(mQRcode))
            return;
        String ssid = MSmartSDK.getInstance().getThirdManager().getSSIDFromQRCode(mQRcode);
        if(NetUtil.containSsid(ssid,mContext)){
            goToConnecting(mQRcode,routerSsid,routerPw);
        }else {
            goToNoFoundDevice();
        }
    }

    private void goToConnecting(String mQRcode,String routerSsid,String routerPw){
        SmartConnectingFrg connectionFrg = new SmartConnectingFrg();
        Bundle bun = new Bundle();
        ConfigureArg arg = new ConfigureArg(mQRcode,routerSsid,routerPw);
        bun.putSerializable("arg",arg);
        connectionFrg.setArguments(bun);
        mContext.changeFragment(mContext.getSupportFragmentManager(), connectionFrg, SmartConnectingFrg.class, true);
    }

    public void goToNoFoundDevice() {
        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_nofindappwifi.getStringValue());
        SmartNoFoundDeviceFrg nfDeviceFrg = new SmartNoFoundDeviceFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(), nfDeviceFrg, SmartNoFoundDeviceFrg.class, true);
    }


}



